package com.highspeed.bdk.controller.physical.service;

import com.highspeed.bdk.TestMainApplication;
import com.highspeed.bdk.bean.entity.ViewPostion;
import com.highspeed.bdk.bean.entity.physical.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {TestMainApplication.class})
@Transactional
public class MachineBoxServiceTester extends BaseDeviceServiceTester {
    private MachineBox machineBox;

    @Before
    public void init() {
        PhysicalTopology pt = createPhysicalTopology();
        physicalTopologyService.saveAndFlush(pt);
        physicalTopologyService.clear();

        MachineBox mb = createMachineBox();
        mb.setPhysicalTopology(pt);
        for (int i = 0; i < 5; i++) {
            Board board = createBoard();
            for (int j = 0; j < 5; j++) {
                Cpu cpu = createCpu();
                board.getCpus().add(cpu);
            }
            mb.getBoards().add(board);
        }
        this.machineBox = machineBoxService.save(mb);
        for (Board board : this.machineBox.getBoards()) {
            createPhysicalLink(board.getCpus().get(0), board.getCpus().get(1));
        }
        machineBoxService.flush();
        machineBoxService.clear();
    }

    @Test(expected = JpaSystemException.class)
    public void saveException() {
        MachineBox entity = createMachineBox();
        machineBoxService.saveAndFlush(entity);
    }

    @Test
    public void save() {
        Assert.assertNotNull(this.machineBox);

        //验证Board数据条数
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineBox.id", this.machineBox.getId());
        Page<Board> result = boardService.query(params, null);
        Assert.assertEquals(result.getContent().size(), 5);

        //验证Cpu数据条数
        params = new HashMap<String, Object>();
        params.put("board.machineBox.id", this.machineBox.getId());
        Page<Cpu> cpus = cpuService.query(params, null);
        Assert.assertEquals(cpus.getContent().size(), 25);
    }

    @Test
    public void deleteLinks() {

        List<PhysicalLink> physicalLinks = physicalLinkService.all();
        List<PhysicalLink> mbLinks = physicalLinkService.findByMachineBoxId(this.machineBox.getId());
        Assert.assertEquals(mbLinks.size(), 5);

        machineBoxService.delete(this.machineBox.getId());
        List<PhysicalLink> nowBbLinks = physicalLinkService.findByMachineBoxId(this.machineBox.getId());
        Assert.assertEquals(nowBbLinks.size(), 0);

        MachineBox tmp = machineBoxService.findOne(this.machineBox.getId());
        Assert.assertNull(tmp);
        List<PhysicalLink> nowLinks = physicalLinkService.all();
        Assert.assertEquals(physicalLinks.size() - mbLinks.size(), nowLinks.size());
    }


    @Test
    public void delete() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("board.machineBox.id", machineBox.getId());
        Page<Cpu> linkCpus = cpuService.query(params, null);
        Assert.assertEquals(linkCpus.getContent().size(), 25);

        cpuService.delete(machineBox.getBoards().get(0).getCpus().get(0).getId());
        Page<Cpu> nowLinkCpus = cpuService.query(params, null);
        Assert.assertEquals(linkCpus.getContent().size() - nowLinkCpus.getContent().size(), 1);

       // List<PhysicalLink> mbLinks = physicalLinkService.findByMachineBoxId(machineBox.getId());
       // Assert.assertEquals(mbLinks.size(), 4);

        params = new HashMap<String, Object>();
        params.put("machineBox.id", machineBox.getId());
        Page<Board> linkBoards = boardService.query(params, null);
        Assert.assertEquals(linkBoards.getContent().size(), 5);
        boardService.delete(machineBox.getBoards().get(1).getId());
        Page<Board> nowLinkBoards = boardService.query(params, null);
        Assert.assertEquals(linkBoards.getContent().size() - nowLinkBoards.getContent().size(), 1);

       // mbLinks = physicalLinkService.findByMachineBoxId(machineBox.getId());
       // Assert.assertEquals(mbLinks.size(), 3);
    }

    @Test
    public void query() {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", machineBox.getId());
        List<MachineBox> list = machineBoxService.queryToList(params, null);
        Assert.assertEquals(list.size(), 1);
    }
}
